/**
 * 作者: wgx
 * 日期: 2024-10-16 11:20:13
 * 备注: 用户武器表
 */
package com.motu.monstercity.server.game.userdata;

import com.motu.monstercity.protocol.Common.PBUserWeapon;
import com.motu.vertx.module.utility.model.BaseModel;
import com.motu.vertx.module.utility.model.Model;
import io.vertx.core.json.JsonObject;
import io.vertx.core.json.JsonArray;

public class UserWeaponModel extends BaseModel implements Model {
    public static final String LISTKEY = "user_id";
    public static final int REDIS_EXPIRE_TIME = 0;// redis过期时间
    public static final int UPDATE_DB_TYPE = UPDATE_DB_TYPE_QUEUE;// 数据入库类型 队列入库
    public static final boolean ORDER_LIST = false;// 是否需要额外的有序列表

    protected long id; // 数据库ID
    protected long userId; // 用户ID
    protected int weaponId; // 武器Id
    protected int level; // 等级
    protected int maxLevel; // 最大等级 (觉醒时增加)
    protected int talent; // 资质 (升级时增加)
    protected int awakeNum; // 已觉醒次数
    protected String attAdd = ""; // 属性加成 (jsonarray格式的字符串)
    protected String attRefresh = ""; // 属性加成刷新出来的 (jsonarray格式的字符串)
    protected int isPutOn; // 是否已装备 (1-已装备)
    protected int partnerId; // 装备的伙伴id
    protected String refreshNum = ""; // 用金币洗练的次数
    protected int attActiveNum; // 属性生效的最高数量,计算成就用
    protected String refreshType = ""; // 洗练类型,数数用 (1--普通洗练  2--高级洗练  3--一键洗练)

    public void reset() {
    }

    protected String getKey() {
        return "UserWeapon." + id;
    }

    protected String getListKey() {
        return "UserWeapon." + userId;
    }

    protected String getListItemKey() {
        return id + "";
    }

    public void doCreate() {
        setTableId();
        insert();
    }

    public void setTableId() {
        putId(incrTableId("user_weapon"));
    }

    public JsonObject insert() {
        return super.insert(getKey(), id, REDIS_EXPIRE_TIME, UPDATE_DB_TYPE, LISTKEY, getListKey(), getListItemKey(), ORDER_LIST);
    }

    public void delete() {
        super.delete(getKey(), id, UPDATE_DB_TYPE, userId, LISTKEY, getListKey(), getListItemKey(), ORDER_LIST);
    }

    /**
     * 根据入库类型更新
     */
    public JsonObject update() {
        return super.update(getKey(), id, REDIS_EXPIRE_TIME, UPDATE_DB_TYPE, userId, getAllObj(), LISTKEY, getListKey(), getListItemKey());
    }

    /**
     * 实时入队列
     */
    public JsonObject updateQueueNow() {
        return super.update(getKey(), id, REDIS_EXPIRE_TIME, UPDATE_DB_TYPE, 0, getAllObj(), LISTKEY, getListKey(), getListItemKey());
    }

    public JsonObject getAllObj(){
        JsonObject obj = new JsonObject();
        obj.put("id", id); // 数据库ID
        obj.put("userId", userId); // 用户ID
        obj.put("weaponId", weaponId); // 武器Id
        obj.put("level", level); // 等级
        obj.put("maxLevel", maxLevel); // 最大等级 (觉醒时增加)
        obj.put("talent", talent); // 资质 (升级时增加)
        obj.put("awakeNum", awakeNum); // 已觉醒次数
        obj.put("attAdd", attAdd); // 属性加成 (jsonarray格式的字符串)
        obj.put("attRefresh", attRefresh); // 属性加成刷新出来的 (jsonarray格式的字符串)
        obj.put("isPutOn", isPutOn); // 是否已装备 (1-已装备)
        obj.put("partnerId", partnerId); // 装备的伙伴id
        obj.put("refreshNum", refreshNum); // 用金币洗练的次数
        obj.put("attActiveNum", attActiveNum); // 属性生效的最高数量,计算成就用
        obj.put("refreshType", refreshType); // 洗练类型,数数用 (1--普通洗练  2--高级洗练  3--一键洗练)
        return obj;
    }

    public PBUserWeapon.Builder takeInitBuilder() {
        PBUserWeapon.Builder sb = PBUserWeapon.newBuilder();
        if (id != 0) {
            sb.setId(id);
        }
        if (weaponId != 0) {
            sb.setWeaponId(weaponId);
        }
        if (level != 0) {
            sb.setLevel(level);
        }
        if (maxLevel != 0) {
            sb.setMaxLevel(maxLevel);
        }
        if (talent != 0) {
            sb.setTalent(talent);
        }
        if (awakeNum != 0) {
            sb.setAwakeNum(awakeNum);
        }
        if (isPutOn != 0) {
            sb.setIsPutOn(isPutOn);
        }
        if (partnerId != 0) {
            sb.setPartnerId(partnerId);
        }
        if (this.refreshNum != null && this.refreshNum.length() > 0) {
            JsonArray list = new JsonArray(this.refreshNum);
            for(int i=0; i<list.size(); i++) {
                sb.addRefreshNum(list.getInteger(i));
            }
        }
        return sb;
    }

    public PBUserWeapon.Builder takeBuilder() {
        if (updateObj == null || updateObj.isEmpty()) {
            return takeInitBuilder();
        }
        PBUserWeapon.Builder sb = PBUserWeapon.newBuilder();
        if (isFieldUpdate("id")) {
            sb.setId(id);
        }
        if (isFieldUpdate("weaponId")) {
            sb.setWeaponId(weaponId);
        }
        if (isFieldUpdate("level")) {
            sb.setLevel(level);
        }
        if (isFieldUpdate("maxLevel")) {
            sb.setMaxLevel(maxLevel);
        }
        if (isFieldUpdate("talent")) {
            sb.setTalent(talent);
        }
        if (isFieldUpdate("awakeNum")) {
            sb.setAwakeNum(awakeNum);
        }
        if (isFieldUpdate("isPutOn")) {
            sb.setIsPutOn(isPutOn);
        }
        if (isFieldUpdate("partnerId")) {
            sb.setPartnerId(partnerId);
        }
        if (isFieldUpdate("refreshNum")) {
            if (this.refreshNum != null && this.refreshNum.length() > 0) {
                JsonArray list = new JsonArray(this.refreshNum);
                for(int i=0; i<list.size(); i++) {
                    sb.addRefreshNum(list.getInteger(i));
                }
                if (list.isEmpty()) {
                    sb.clearRefreshNum();
                }
            } else {
                sb.clearRefreshNum();
            }
        }
        return sb;
    }

    public long getId() {
        return id;
    }

    public void putId(long id) {
        if (this.id != id) {
            this.id = id;
            updateRedisHashField(getKey(), "id", this.id);
        }
    }

    public void addId(long value) {
        this.id += value;
        updateRedisHashField(getKey(), "id", this.id);
    }

    public long getUserId() {
        return userId;
    }

    public void putUserId(long userId) {
        if (this.userId != userId) {
            this.userId = userId;
            updateRedisHashField(getKey(), "userId", this.userId);
        }
    }

    public void addUserId(long value) {
        this.userId += value;
        updateRedisHashField(getKey(), "userId", this.userId);
    }

    public int getWeaponId() {
        return weaponId;
    }

    public void putWeaponId(int weaponId) {
        if (this.weaponId != weaponId) {
            this.weaponId = weaponId;
            updateRedisHashField(getKey(), "weaponId", this.weaponId);
        }
    }

    public void addWeaponId(int value) {
        this.weaponId += value;
        updateRedisHashField(getKey(), "weaponId", this.weaponId);
    }

    public int getLevel() {
        return level;
    }

    public void putLevel(int level) {
        if (this.level != level) {
            this.level = level;
            updateRedisHashField(getKey(), "level", this.level);
        }
    }

    public void addLevel(int value) {
        this.level += value;
        updateRedisHashField(getKey(), "level", this.level);
    }

    public int getMaxLevel() {
        return maxLevel;
    }

    public void putMaxLevel(int maxLevel) {
        if (this.maxLevel != maxLevel) {
            this.maxLevel = maxLevel;
            updateRedisHashField(getKey(), "maxLevel", this.maxLevel);
        }
    }

    public void addMaxLevel(int value) {
        this.maxLevel += value;
        updateRedisHashField(getKey(), "maxLevel", this.maxLevel);
    }

    public int getTalent() {
        return talent;
    }

    public void putTalent(int talent) {
        if (this.talent != talent) {
            this.talent = talent;
            updateRedisHashField(getKey(), "talent", this.talent);
        }
    }

    public void addTalent(int value) {
        this.talent += value;
        updateRedisHashField(getKey(), "talent", this.talent);
    }

    public int getAwakeNum() {
        return awakeNum;
    }

    public void putAwakeNum(int awakeNum) {
        if (this.awakeNum != awakeNum) {
            this.awakeNum = awakeNum;
            updateRedisHashField(getKey(), "awakeNum", this.awakeNum);
        }
    }

    public void addAwakeNum(int value) {
        this.awakeNum += value;
        updateRedisHashField(getKey(), "awakeNum", this.awakeNum);
    }

    public String getAttAdd() {
        return attAdd;
    }

    public void putAttAdd(String attAdd) {
        if (!this.attAdd.equals(attAdd)) {
            this.attAdd = attAdd;
            updateRedisHashField(getKey(), "attAdd", this.attAdd);
        }
    }

    public String getAttRefresh() {
        return attRefresh;
    }

    public void putAttRefresh(String attRefresh) {
        if (!this.attRefresh.equals(attRefresh)) {
            this.attRefresh = attRefresh;
            updateRedisHashField(getKey(), "attRefresh", this.attRefresh);
        }
    }

    public int getIsPutOn() {
        return isPutOn;
    }

    public void putIsPutOn(int isPutOn) {
        if (this.isPutOn != isPutOn) {
            this.isPutOn = isPutOn;
            updateRedisHashField(getKey(), "isPutOn", this.isPutOn);
        }
    }

    public void addIsPutOn(int value) {
        this.isPutOn += value;
        updateRedisHashField(getKey(), "isPutOn", this.isPutOn);
    }

    public int getPartnerId() {
        return partnerId;
    }

    public void putPartnerId(int partnerId) {
        if (this.partnerId != partnerId) {
            this.partnerId = partnerId;
            updateRedisHashField(getKey(), "partnerId", this.partnerId);
        }
    }

    public void addPartnerId(int value) {
        this.partnerId += value;
        updateRedisHashField(getKey(), "partnerId", this.partnerId);
    }

    public JsonArray getRefreshNumArray() {
        if (this.refreshNum != null && this.refreshNum.length() > 0) {
            return new JsonArray(this.refreshNum);
        } else {
            return new JsonArray();
        }
    }

    public String getRefreshNum() {
        return refreshNum;
    }

    public void putRefreshNum(String refreshNum) {
        if (!this.refreshNum.equals(refreshNum)) {
            this.refreshNum = refreshNum;
            updateRedisHashField(getKey(), "refreshNum", this.refreshNum);
        }
    }

    public int getAttActiveNum() {
        return attActiveNum;
    }

    public void putAttActiveNum(int attActiveNum) {
        if (this.attActiveNum != attActiveNum) {
            this.attActiveNum = attActiveNum;
            updateRedisHashField(getKey(), "attActiveNum", this.attActiveNum);
        }
    }

    public void addAttActiveNum(int value) {
        this.attActiveNum += value;
        updateRedisHashField(getKey(), "attActiveNum", this.attActiveNum);
    }

    public JsonArray getRefreshTypeArray() {
        if (this.refreshType != null && this.refreshType.length() > 0) {
            return new JsonArray(this.refreshType);
        } else {
            return new JsonArray();
        }
    }

    public String getRefreshType() {
        return refreshType;
    }

    public void putRefreshType(String refreshType) {
        if (!this.refreshType.equals(refreshType)) {
            this.refreshType = refreshType;
            updateRedisHashField(getKey(), "refreshType", this.refreshType);
        }
    }

}